package com.superjson.showpro.shared.util;

import com.alibaba.fastjson.JSONObject;
import com.superjson.showpro.shared.enums.TitleEnum;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;

import static java.math.BigDecimal.*;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * Created By zby on 20:39 2019/5/30
 * 处理json格式的字符串
 */

public class JsonUtil {

    /**
     * Created By zby on 20:41 2019/5/30
     * 将枚举的值以key value的方式填充
     */
    public static JSONObject getJsonObject(Object o, String... methodNames) {
        JSONObject json = null;
        if (o == null || ArrayUtils.isEmpty(methodNames)) {
            return json;
        }
        json = new JSONObject();
        json.put("key", o);
        Class var = o.getClass();
        for (String methodName : methodNames) {
            try {
                if (isBlank(methodName)) {
                    continue;
                }
                String var1;
                if (methodName.startsWith("get")) {
                    var1 = methodName.substring(ROUND_FLOOR);

                } else if (methodName.startsWith("is")) {
                    var1 = methodName.substring(ROUND_CEILING);
                } else {
                    throw new RuntimeException("方法名错误，只能以set或isk开头【methodName】=" + methodName);
                }
                var1 = var1.substring(ROUND_UP, ROUND_DOWN).toLowerCase() + var1.substring(ROUND_DOWN);
                json.put(var1, var.getDeclaredMethod(methodName).invoke(o,null));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return json;
    }
}
